package com.china08.yxyapi.api.manage.sch.platform.xiaoben.kecheng;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class KeChengLessonStatusUpdController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonRepository lessonRepository;

    @Autowired
    YxyLessonsSchoolLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/kecheng/lesson/status/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.id;
        Integer status = reqModel.status;
        String rejectReason = reqModel.rejectReason;
        Integer duration = 0;

        YxyLessonsSchoolLesson lesson = lessonRepository.findById(lessonId);
        if (lesson != null) {
            lesson.setStatus(status);
            lesson.setRejectReason(rejectReason);

            // 审核拒绝
            if (status == 3) {
                // 设置审核拒绝时间
                lesson.setRejectDate(new Date());
            }

            // 课程通过
            if (status == 2) {
                // 课程通过，课程上架
                lesson.setIsVisible(1);
                // 设置上架时间
                lesson.setUpDate(new Date());

                // 获取课程所有课时
                List<YxyLessonsSchoolLessonItem> itemList = itemRepository.findByLessonId(lessonId);
                for (YxyLessonsSchoolLessonItem item : itemList) {
                    // 获取视频时长
                    YxyLessonsVideo video = videoRepository.findById(item.getVideoId());
                    if (video != null) {
                        duration = duration + video.getDuration();
                    }

                    // 课时未删除状态
                    if (item.getStatus() != -1) {
                        item.setStatus(status);
                        item.setRejectReason(rejectReason);
                        itemRepository.save(item);
                    }
                }
                lesson.setDuration(duration);
            }
            lessonRepository.save(lesson);
        }
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String id;
        public Integer status;
        public String rejectReason;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "未获取课程");
        }
    }
}
